package com.trip.wechat.platform.service.impl;

import com.trip.wechat.platform.model.MessageBO;
import com.trip.wechat.platform.model.user.UserBO;
import com.trip.wechat.platform.model.user.UserList;
import com.trip.wechat.platform.service.MessageService;
import com.trip.wechat.platform.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;

/**
 * 功能说明: <br>
 * 系统说明: <br>
 * 模块说明: <br>
 * 功能描述: <br>
 */
@Service
public class MessageServiceImpl implements MessageService {

    static final String TYPE_EVENT = "event";
    static final String EVENT_SUBSCRIBE = "subscribe";  // 订阅
    static final String EVENT_UNSUBSCRIBE = "unsubscribe";  // 取消订阅

    @Autowired
    UserService userService;

    @Override
    public void dispatch(MessageBO messageBO) {
        if (TYPE_EVENT.equals(messageBO.getMsgType())) {
            if (EVENT_SUBSCRIBE.equals(messageBO.getEvent())) {             // 订阅
                UserList userList = new UserList();
                userList.setPublicKey(messageBO.getToUserName());
                userList.setOpenid(Collections.singletonList(messageBO.getFromUserName()));

                userService.submitTask(userList);
            } else if (EVENT_UNSUBSCRIBE.equals(messageBO.getEvent())) {    // 取消订阅
                UserBO userBO = new UserBO();
                userBO.setPublicKey(messageBO.getToUserName());
                userBO.setOpenid(messageBO.getFromUserName());
                userBO.setSubscribe(0);
                userService.syncUserInfo(userBO);
            }
        }
    }
}
